<?php
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\wiki\WikiContent as mWikiContent;
use Doraemon\tools\Curl;

$url = 'https://www.samsungeshop.com.cn/product/QA55LS01CAJXXZ?cid=cn_paid_display_marketing_ctv-ls01c_estore_qa55ls01cajxxz_other_hhz-220921_none';
$arr = [
    'https://shop.samsung.com.cn/product/QA55LS03CAJXXZ/QA55LS03CAJXXZ?cid=cn_paid_display_hhz_mktg-ctv_estore_qa55ls03cajxxz_other_230613_none',
    'https://shop.samsung.com.cn/product/QA55S95ZAJXXZ/QA55S95ZAJXXZ?cid=cn_paid_display_hhz_mktg-ctv_estore_qa55s95zajxxz_other_230613_none',
    'https://shop.samsung.com.cn/product/QA75QN900CJXXZ/QA75QN900CJXXZ?cid=cn_paid_display_hhz_mktg-ctv_estore_qa75qn900cjxxz_other_230613_none'
];
foreach ($arr as $url) {
    createWiki($url);
}

function createWiki($url)
{
    $brandId = 0;//todo 是否有品牌？
    $spuId = getSpuId($url);
    if (empty($spuId)) {
        var_dump('获取spuId失败');
        return;
    }
    $content = getContent($spuId);
    $info = getInfo($spuId);
    list($coverImg, $imgs) = getImgList($spuId);
    if ($info) {
        $info['brand_id'] = $brandId;
        $info['cover_img'] = $coverImg;
        $wikiId = create($info, $content, $imgs);
        var_dump('创建wiki成功，wiki_id:' . $wikiId);
    } else {
        var_dump('获取基础信息失败');
    }
}

function getSpuId($url)
{
    $path = parse_url($url, PHP_URL_PATH);
    $path = str_replace('/product/', '', $path);
    $arr = explode('/', $path);
    $spuId = current($arr);
    return $spuId;
}

function getInfo($spuId)
{
    $info = [];
    $url = 'https://www.samsungeshop.com.cn/api/product/pdp/baseInfo/' . $spuId;
    $result = Curl::Request('GET', $url);
    if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
        if (isset($result['json']['data']) && $result['json']['data']) {
            $data = $result['json']['data'];
            $info = [
                'title' => $data['prodTitl'],
                'channel' => mWiki::CHANNEL_SAMSUNGESHOP,
                'channel_type' => mWiki::CHANNEL_TYPE_SAMSUNGESHOP,
                'channel_jump_params' => $spuId,
                'channel_brand' => '三星',
                'channel_shop_id' => 0,
                'channel_shop_name' => '',
                'channel_spu_id' => $data['spuId'],
                'channel_category_name' => $data['catgLvlOneNm'],
                'category_id' => 0,
                'second_category_id' => 0,
                'last_category_id' => 0,
                'cover_img' => '',
                'min_price' => $data['skuMinPrice'],
                'max_price' => $data['skuMaxPrice'],
                'status' => mWiki::STATUS_WAIT_CHECK,
            ];
        }
    }
    return $info;
}

function getImgList($spuId)
{
    $imgs = '';
    $coverImg = '';
    $url = 'https://www.samsungeshop.com.cn/api/product/h5/pdp/leftInfo/' . $spuId;
    $result = Curl::Request('GET', $url);
    if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
        if (isset($result['json']['data']) && $result['json']['data']) {
            $data = $result['json']['data']['pdpSkuImagesInfoList'];
            $data = current($data);
            $imgsList = [];
            $imgsUrls = $data['imgUrl'] ? explode(',', $data['imgUrl']) : [];
            foreach ($imgsUrls as $key => $row) {
                $arr = explode('.', $row);
                if (strtolower(end($arr)) == "mp4") {
                    continue;
                }
                $imgsList[] = $row;
            }
            $coverImg = current($imgsList);
            $imgs = json_encode($imgsList);
        }
    }
    return [$coverImg, $imgs];
}

function getContent($spuId)
{
    $content = '';
    $url = 'https://www.samsungeshop.com.cn/api/product/pdp/bottom/detailInfo/' . $spuId;
    $result = Curl::Request('GET', $url);
    if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
        if (isset($result['json']['data']) && $result['json']['data']) {
            $data = $result['json']['data']['pdpspuDetailImagesInfoList'];
            foreach ($data as $row) {
                if ($row['chCd'] == 2 && $row['imgUrl']) {
                    $arr = explode('.', $row['imgUrl']);
                    if (strtolower(end($arr)) == "mp4") {
                        continue;
                    }
                    $content .= '<img src="' . $row['imgUrl'] . '">';
                }
            }
        }
    }
    return $content;
}


function create($data, $content, $imgs)
{
    $contentData = [
        'imgs' => $imgs,
        'content' => $content,
    ];
    $wikiInfo = mWiki::getInfoByChannelSpu(mWiki::CHANNEL_SAMSUNGESHOP, $data['channel_spu_id']);
    if ($wikiInfo) {
        $wikiId = $wikiInfo['id'] ?? 0;
        mWiki::updateData($data, ['id' => $wikiId]);
        $contentInfo = mWikiContent::getByWhere(['wiki_id' => $wikiId]);
        if ($contentInfo) {
            if ($wikiInfo['status'] != mWiki::STATUS_CHECKED) {
                if ($contentInfo['imgs'] == $contentData['imgs']) unset($contentData['imgs']);
                if ($contentInfo['content'] == $contentData['content']) unset($contentData['content']);
                if ($contentData) mWikiContent::updateData($contentData, ['wiki_id' => $wikiId]);
                var_dump(mWikiContent::log());
            }
        } else {
            $contentData['wiki_id'] = $wikiId;
            mWikiContent::insertData($contentData);
        }
    } else {
        $wikiBasicData = [
            'brand_id' => $data['brand_id'],
            'title' => $data['title'],
            'basic_state' => mWikiBasic::STATE_NORMAL,
            'category_id' => $data['category_id'],
            'second_category_id' => $data['second_category_id'],
            'last_category_id' => $data['last_category_id'],
        ];
        $basicId = mWikiBasic::insertData($wikiBasicData);
        $data['basic_id'] = $basicId;
        $data['source_basic_id'] = $basicId;
        $data['is_default'] = 1;
        $wikiId = mWiki::insertData($data);
        $contentData['wiki_id'] = $wikiId;
        mWikiContent::insertData($contentData);
    }
    return $wikiId;
}